//app.js
import config from "./config";
import { getAllmidList } from  "./utils/api/module/cyfw";
App({
    onLaunch: function () {
        // 展示本地存储能力
        // var logs = wx.getStorageSync('logs') || []
        // logs.unshift(Date.now())
        // wx.setStorageSync('logs', logs)

        // 登录
        // wx.login({
        //     success: res => {
        //         // 发送 res.code 到后台换取 openId, sessionKey, unionId
        //     }
        // })
        // 获取用户信息
        // wx.getSetting({
        //     success: res => {
        //         if (res.authSetting['scope.userInfo']) {
        //             // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
        //             wx.getUserInfo({
        //                 success: res => {
        //                     // 可以将 res 发送给后台解码出 unionId
        //                     this.globalData.userInfo = res.userInfo

        //                     // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
        //                     // 所以此处加入 callback 以防止这种情况
        //                     if (this.userInfoReadyCallback) {
        //                         this.userInfoReadyCallback(res)
        //                     }
        //                 }
        //             })
        //         }
        //     }
        // })
        this.getAllmidList();
        this.loginStatus()
    },
    defaultImg:{
        yqImg: 'https://ythapi.bjyangshu.com/uploads/park_default.jpg',
        wzImg: 'https://ythapi.bjyangshu.com/uploads/news_default.jpg',
    },
    image: 'https://ythapi.bjyangshu.com/uploads/news_default.jpg',
    globalData: {
        userInfo: null,
        config,
    },
    // 热门活动
    activityData: [],
    // 状态栏重写
    statusBarHeight:  wx.getSystemInfoSync(),
    // 获取多个用户ID && 获取热门活动
    getAllmidList: function() {
        getAllmidList().then(res => {
            let data = res.data.data;
            data.map((item, index) => {
                this.getActivity(item.mid, index);
            });
        });
    },
    getActivity: function(id, index) {
        let timestamp = new Date().getTime(),
            md5 = require('md5'),
            sign = md5('6aa8ebbe20|5351c9d0734c4070825209066df99088|3.0|' + timestamp);
        wx.request({
            url: 'https://openapi.hudongba.com/v3/info/userTimeline',
            method: 'POST',
            data: {
                foreignUserId: id,
                pageNumber: 1,
                pageSize: 5,
                sign: sign,
                timestamp: timestamp,
                version: '3.0',
                appId: '6aa8ebbe20'
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            dataType: 'json',
            success: res => {
                this.activityData[index] = res.data.result.infoList
            },
        });
    },
    loginStatus: function() {
        let timer = wx.getStorageSync('loginTimer'),
            currentTimer = (new Date()).getTime(),
            end = Number(timer) + 24 * 60 * 60 * 1000;
            console.log()
        if(currentTimer > end && timer != undefined || !timer) {
            wx.clearStorageSync()
        };
    }
})